import { defineStore } from 'pinia';
import { store } from '@/store';
import { getGlobalConfig } from '@/api/common';

export interface InConfig {
  responseForeverOk?: boolean;
  tokenEffectiveHour?: number;
  enableRegister?: boolean;
  defaultPageSize?: number;
  enableCaptcha?: boolean;
  captchaType?: string;
}

export interface IConfigState {
  config: InConfig;
}

export const useConfigStore = defineStore({
  id: 'app-config',
  state: (): IConfigState => ({
    config: {},
  }),
  getters: {
    getConfig(): InConfig {
      return this.config;
    },
  },
  actions: {
    setConfig(config: InConfig) {
      this.config = config;
    },
    // 获取配置
    async requestConfig() {
      const configRes = await getGlobalConfig();
      this.setConfig(configRes.config);
    },
  },
});

// Need to be used outside the setup
export function useConfigStoreWidthOut() {
  return useConfigStore(store);
}
